package com.fms.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fms.common.vo.Result;
import com.fms.sys.entity.SiteCategory;
import com.fms.sys.service.ISiteCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 站点分类控制器
 * </p>
 *
 * @author Jinfeng
 * @since 2024-03-23
 */
@RestController
@RequestMapping("/site/category")
public class SiteCategoryController {
    @Autowired
    private ISiteCategoryService siteCategoryService;

    @GetMapping("/list")
    public Result<Map<String, Object>> getCategoryList(
            @RequestParam(value = "pageNo", defaultValue = "1") Long pageNo,
            @RequestParam(value = "pageSize", defaultValue = "10") Long pageSize,
            @RequestParam(value = "categoryName", required = false) String categoryName) {

        LambdaQueryWrapper<SiteCategory> wrapper = new LambdaQueryWrapper<>();
        if (categoryName != null && !categoryName.isEmpty()) {
            wrapper.like(SiteCategory::getCategoryName, categoryName);
        }
        wrapper.orderByAsc(SiteCategory::getSortOrder);

        Page<SiteCategory> page = new Page<>(pageNo, pageSize);
        siteCategoryService.page(page, wrapper);

        Map<String, Object> data = new HashMap<>();
        data.put("total", page.getTotal());
        data.put("rows", page.getRecords());

        return Result.success(data);
    }

    @GetMapping("/all")
    public Result<List<SiteCategory>> getAllCategories() {
        LambdaQueryWrapper<SiteCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(SiteCategory::getSortOrder);
        return Result.success(siteCategoryService.list(wrapper));
    }

    @PostMapping
    public Result<?> addCategory(@RequestBody SiteCategory category) {
        siteCategoryService.save(category);
        return Result.success("新增分类成功");
    }

    @PutMapping
    public Result<?> updateCategory(@RequestBody SiteCategory category) {
        siteCategoryService.updateById(category);
        return Result.success("修改分类成功");
    }

    @DeleteMapping("/{id}")
    public Result<?> deleteCategory(@PathVariable("id") Long id) {
        // 检查是否为系统分类
        SiteCategory category = siteCategoryService.getById(id);
        if (category != null && category.getIsSystem()) {
            return Result.fail("系统分类不能删除");
        }
        if (!siteCategoryService.removeById(id)) {
            return Result.fail("删除失败，分类不存在");
        }
        return Result.success("删除分类成功");
    }

    @PutMapping("/sort")
    public Result<?> updateSort(@RequestBody List<SiteCategory> categories) {
        for (SiteCategory category : categories) {
            SiteCategory update = new SiteCategory();
            update.setId(category.getId());
            update.setSortOrder(category.getSortOrder());
            siteCategoryService.updateById(update);
        }
        return Result.success("更新排序成功");
    }
}